/* hashkv.h : dgnsc hash key/value map
 * Copyright (C) 2011 drangon <drangon.zhou@gmail.com>
 * 2011-09
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_DGNSC_HASHKV_H
#define INCLUDED_DGNSC_HASHKV_H

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

typedef struct dgn_hashkv_st * dgn_hashkv_t;
typedef struct dgn_hashkv_item_st * dgn_hashkv_iter_t;

typedef unsigned int (*dgn_hashkv_hash_func_t)( const void * key );
typedef int (*dgn_hashkv_cmp_func_t)( const void * key1, const void * key2 );
typedef void (*dgn_hashkv_freek_func_t)( void * key );
typedef void (*dgn_hashkv_freev_func_t)( void * val );

// default, key is string (char *)
unsigned int dgn_hashkv_default_hash( const void * key ); // string hash
int dgn_hashkv_default_cmp( const void * key1, const void * key2 ); // strcmp()
void dgn_hashkv_default_free( void * korv ); // just free()

dgn_hashkv_t dgn_hashkv_init( dgn_hashkv_hash_func_t hash_fn, 
		dgn_hashkv_cmp_func_t cmp_fn, dgn_hashkv_freek_func_t freek_fn,
		dgn_hashkv_freev_func_t freev_fn );
dgn_hashkv_t dgn_hashkv_init_default();
void dgn_hashkv_fini( dgn_hashkv_t hkv );


// key/value is owned and free by hashkv,
// if key exist, old key/value will be free or return
int dgn_hashkv_add( dgn_hashkv_t hkv, void * key, void * value );
int dgn_hashkv_add_ex( dgn_hashkv_t hkv, void * key, void * value, void ** old_key, void ** old_value );

// set exist key, if not exist, do nothing
int dgn_hashkv_set( dgn_hashkv_t hkv, const void * key, void * value, void ** old_value );

// delete and free local key/value if exist, param key not touch
int dgn_hashkv_del( dgn_hashkv_t hkv, const void * key );
int dgn_hashkv_del_ex( dgn_hashkv_t hkv, const void * key, void ** ret_key, void ** ret_value );
void dgn_hashkv_clear( dgn_hashkv_t hkv );

// find value, if not exist, return NULL, may be confuse
// use dgn_hashkv_iter_find() if want to know exist or not
void * dgn_hashkv_get( dgn_hashkv_t hkv, const void * key );
int dgn_hashkv_item_num( dgn_hashkv_t hkv );

// iterator, NULL means not exist, be invalid if call above modify func
dgn_hashkv_iter_t dgn_hashkv_iter_next( dgn_hashkv_t hkv, dgn_hashkv_iter_t iter );
dgn_hashkv_iter_t dgn_hashkv_iter_find( dgn_hashkv_t hkv, const void * key );
void * dgn_hashkv_iter_get_key( dgn_hashkv_t hkv, dgn_hashkv_iter_t iter );
void * dgn_hashkv_iter_get( dgn_hashkv_t hkv, dgn_hashkv_iter_t iter );
int dgn_hashkv_iter_set( dgn_hashkv_t hkv, dgn_hashkv_iter_t iter, void * value, void ** old_value );
dgn_hashkv_iter_t dgn_hashkv_iter_del( dgn_hashkv_t hkv, dgn_hashkv_iter_t iter, void ** ret_key, void ** ret_value );

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // INCLUDED_DGNSC_HASHKV_H

